package com.iwinding.ladygg.ui.view;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.widget.chatrow.EaseChatRow;
import com.iwinding.ladygg.R;
import com.iwinding.ladygg.modle.bean.EMSosMessageBean;
import com.iwinding.ladygg.net.response.LoginRp;
import com.iwinding.ladygg.ui.act.SosDetailActivity;
import com.iwinding.ladygg.utils.DateUtil;
import com.iwinding.ladygg.utils.SystemUtil;

/**
 * Created by hezhiyong on 2017/4/21.
 */

public class ChatRowSosCall extends EaseChatRow {


    private TextView mTime;
    private TextView mName;
    private TextView mPhone;
    private TextView mLocation;

    private EMSosMessageBean bean;


    public ChatRowSosCall(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    protected void onInflateView() {
        int type = message.getIntAttribute(EMSosMessageBean.Attribute.VALUES[0], 0);
        if (type == 0) {
            inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                    R.layout.icane_view_sos_do : R.layout.icane_view_sos_send, this);
        } else {
            inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                    R.layout.icane_view_sos_do2 : R.layout.icane_view_sos_send2, this);
        }
    }

    @Override
    protected void onFindViewById() {
        mName = (TextView) findViewById(R.id.icane_sos_txt_name);
        mTime = (TextView) findViewById(R.id.icane_sos_txt_time);
        mPhone = (TextView) findViewById(R.id.icane_sos_txt_phone);
        mLocation = (TextView) findViewById(R.id.icane_sos_txt_location);
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onSetUpView() {
        String type = message.getStringAttribute(EMSosMessageBean.Attribute.VALUES[0], "");
        if (!TextUtils.isEmpty(type)) {
            bean = new EMSosMessageBean();
            bean.setAttribute(message);
        }
        if (bean == null) {
            return;
        }
        if (mName != null) {
            mName.setText(bean.uname);
        }
        if (mTime != null) {
            String helpTime = bean.helpTime;
            String time = bean.time;
            try {
                helpTime = DateUtil.getTime(bean.helpTime);
                if (!TextUtils.isEmpty(time))
                    time = DateUtil.getTime(time);
            } catch (Exception e) {
            }
            mTime.setText(TextUtils.equals(bean.type, "0") ? time : helpTime);
        }
        if (mPhone != null) {
            mPhone.setText(bean.mobile);
            if (TextUtils.equals(type, "1"))
                mPhone.setText(bean.helper);
        }
        if (mLocation != null) {
            mLocation.setText(bean.address);
        }
    }

    @Override
    protected void onBubbleClick() {
        if (bean == null) {
            return;
        }
        String type = bean.type;
        if (!TextUtils.isEmpty(type) && Integer.parseInt(type) == 0) {
            LoginRp loginRp = SystemUtil.getLoginRp(getContext());
            if (loginRp == null) {
                return;
            }
            Intent detail = new Intent(getContext(), SosDetailActivity.class);
            detail.putExtra(SosDetailActivity.SOSID, String.valueOf(bean.sosID));
            boolean isme = false;
            if (!TextUtils.isEmpty(loginRp.userId) && !TextUtils.isEmpty(bean.userID) && TextUtils.equals(loginRp.userId, bean.userID)) {
                isme = true;
            }
            detail.putExtra(SosDetailActivity.ISME, isme);
            getContext().startActivity(detail);
        }
    }

}
